import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/controller/knowledgeword.dart';
import 'package:ichinesereader/widgets/cards/word.dart';
import 'package:ichinesereader/widgets/layouts/customAppBar.dart';

class KnowledgeWordsPage extends StatefulWidget {
  KnowledgeWordsPage({Key? key, this.bookCode}) : super(key: key);
  final String? bookCode;
  static const String routeName = '/common/flashcards';

  @override
  State<KnowledgeWordsPage> createState() => _KnowledgeWordsPageState();
}

class _KnowledgeWordsPageState extends State<KnowledgeWordsPage> {
  final KnowledgeWordsController _controller =
      Get.put(KnowledgeWordsController());

  @override
  void initState() {
    if (this.widget.bookCode == null)
      _controller.fetchBasicKnowledgeWords();
    else
      _controller.fetchKnowledgeWordsByBookcode(
          this.widget.bookCode.toString(), true, true);
    super.initState();
  }

  @override
  void dispose() {
    Get.delete<KnowledgeWordsController>();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: CustomAppBar(title: 'Word Cards'.tr),
        body: Container(
          padding: const EdgeInsets.all(20.0),
          child: Obx(() {
            return GridView.builder(
                padding: EdgeInsets.all(0),
                gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
                    maxCrossAxisExtent: 300,
                    childAspectRatio: 156 / 200,
                    crossAxisSpacing: 20,
                    mainAxisSpacing: 20),
                itemCount: _controller.knowledgeWords.length,
                itemBuilder: (context, index) {
                  return WordCard(flashcard: _controller.knowledgeWords[index]);
                });
          }),
        ));
  }
}
